package com.zlq.friends;

import android.app.ActionBar;
import android.content.Context;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhj on 2016/1/22.
 */
public class MonmentAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    List<Monment> data;
    Context context;
    private final int HEAD_TYPE = 0;
    private final int ITEM_TYPE = 1;
    ScreenUtil screenUtil;
    ArrayList<Comment> arrayList;
    private int marginLeft,marginRight,marginTop,marginBottom;
    private LinearLayout.LayoutParams marginLayoutParame;
    private OnItemClickListener onItemClickListener;
    private OnHeadClickListener onHeadClickListener;
    private ItemViewClickListner itemViewClickListner;
    private View view;
    private LinearLayout layout;
    public MonmentAdapter(List<Monment> data, Context context) {
        this.data = data;
        this.context = context;
        //在这里获取屏幕的各种信息
        screenUtil = new ScreenUtil(context);
        //marginLeft = screenUtil.dip2px()
        marginLayoutParame = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1);
        //设置布局的相对于上下左右的距离
        marginLayoutParame.setMargins(1,1,1,1);
        onHeadClickListener = new OnHeadClickListener();
        itemViewClickListner = new ItemViewClickListner();
    }

    public MonmentAdapter(List<Monment> data, Context context,View view) {
        this.data = data;
        this.context = context;
        this.view = view;
        //在这里获取屏幕的各种信息
        screenUtil = new ScreenUtil(context);
        //marginLeft = screenUtil.dip2px()
        marginLayoutParame = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1);
        //设置布局的相对于上下左右的距离
        marginLayoutParame.setMargins(1,1,1,1);
        onHeadClickListener = new OnHeadClickListener();
        itemViewClickListner = new ItemViewClickListner();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType==HEAD_TYPE){
            View headViewLayot = LayoutInflater.from(context).inflate(R.layout.head_layout,null);
            return new ViewHolderHead(headViewLayot);

        }else if (viewType==ITEM_TYPE){
            View ItemLayout = LayoutInflater.from(context).inflate(R.layout.re_item,null);
            return new ViewHolderItem(ItemLayout);
        }else {
            return null;
        }




    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        /*判断是头部还是条目的类型*/
        if (position!=HEAD_TYPE){
            Monment monment = data.get(position-1);
            ViewHolderItem holderItem = (ViewHolderItem) holder;
            /*View viewItem = holderItem.itemView;*/
            holderItem.item_monment.setText(monment.getMontent());
            holderItem.item_nick_name.setText(monment.getNickName());
            holderItem.item_time_monment.setText(monment.getpTime());
            holderItem.item_click_of_like.setTag(position);
            holderItem.item_click_of_like.setOnClickListener(itemViewClickListner);
            holderItem.item_monment_conment.setTag(position);
            holderItem.item_monment_conment.setOnClickListener(itemViewClickListner);
            layout = holderItem.monment_layout;
        }

    }
    private void setSendInfo(){
        view = LayoutInflater.from(context).inflate(R.layout.add_comment,null);

        Button send_comment = (Button)view.findViewById(R.id.send_comment);
        arrayList = new ArrayList<>();
        send_comment.setOnClickListener(itemViewClickListner);
    }
    private void addComment(ArrayList<Comment> comments){
        if (comments.size()>0){
            for (Comment c: comments) {
                TextView display_comment = new  TextView(context);
                display_comment.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                display_comment.setTextSize(16);
                display_comment.setPadding(5,5,5,5);
                display_comment.setLineSpacing(3, (float) 1.5);
                display_comment.setText(c.getComment());
                layout.addView(display_comment);
            }
        }else {
            layout.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return data==null?1:data.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return HEAD_TYPE;
        }else {
            return ITEM_TYPE;
        }
    }

    @Override
    public void onClick(View v) {
        int postion = Integer.valueOf(v.getTag().toString())-1;
        if (onItemClickListener == null){
            onItemClickListener.onItemClick(v,postion);
        }
    }
    public interface OnItemClickListener{
        void onItemClick(View v,int postion);
    }
    /**
     * 设置条目的点击事件
     *
     * @param onItemClickListener
     * **/
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }


    /**头部**/
    class ViewHolderHead extends RecyclerView.ViewHolder{
        private ImageView head_icon;
        public ViewHolderHead(View itemView) {
            super(itemView);
            head_icon = (ImageView) itemView.findViewById(R.id.icon_head);
        }
    }


    /**头部的点击事件**/
    public class OnHeadClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {

        }
    }
    /**条目的界面的点击事件**/
    private final class ItemViewClickListner implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.click_of_like:
                    Log.e("TAG","点赞"+v.getTag());
                    Toast.makeText(context,"点击了"+v.getTag(),Toast.LENGTH_SHORT).show();
                    setSendInfo();
                    break;
                case R.id.item_monment_conment:
                    Log.e("TAG","评论"+v.getTag());
                    Toast.makeText(context,"点击了",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.send_comment:
                     EditText input_comment = (EditText) view.findViewById(R.id.input_comment);
                     Toast.makeText(context,"点击了",Toast.LENGTH_SHORT).show();
                     String s =  input_comment.getText().toString();
                     Comment con = new Comment(s);
                     arrayList.add(con);
                     addComment(arrayList);
                /*view.setVisibility(View.GONE);*/

                    break;
            }
        }
    }
    /*条目 每个人发布心情的条目*/
    class ViewHolderItem extends RecyclerView.ViewHolder{
        private View itemView;
        private TextView item_time_monment;//显示时间的TextView
        private TextView item_nick_name;//显示昵称的TextView
        private ImageView icon_image;//显示按钮
        private TextView item_monment;//显示说说的TextView
        private CheckBox item_click_of_like;//点赞的按钮
        private ImageButton item_monment_conment;//评论的按钮
        private LinearLayout monment_layout;//显示评论的线性布局
        public ViewHolderItem(View itemView) {
            super(itemView);
            this.itemView = itemView;
            item_time_monment = (TextView) itemView.findViewById(R.id.monment_time);
            item_nick_name = (TextView) itemView.findViewById(R.id.item_nickName);
            item_monment = (TextView) itemView.findViewById(R.id.item_monment);
            icon_image = (ImageView) itemView.findViewById(R.id.icon_img);
            item_click_of_like = (CheckBox) itemView.findViewById(R.id.click_of_like);
            item_monment_conment = (ImageButton) itemView.findViewById(R.id.item_monment_conment);
            monment_layout = (LinearLayout) itemView.findViewById(R.id.monment_layout);

        }
    }
}
